package edu.eam.vpuwar.servlet;

import java.io.IOException;
import java.io.OutputStream;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.eam.vpuejb.ejbs.ProductoEJB;
import edu.eam.vpujpa.entidades.Producto;

/**
 * Servlet implementation class fotoProductoServlet
 */
@WebServlet("/fotoProductoServlet")
public class FotoProductoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      @EJB
      ProductoEJB productoEJB;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FotoProductoServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doall(request,response);
	}

	private void doall(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		long idProducto = Long.parseLong(request.getParameter("idProducto"));
		OutputStream out;
		try {
				Producto producto= productoEJB.buscarProducto(idProducto);
				out= response.getOutputStream();
				out.write(producto.getFoto());
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
